package com.hdkj.eduservice.controller;

import com.hdkj.commonutils.JwtUtils;
import com.hdkj.commonutils.R;
import com.hdkj.eduservice.entity.EduArtcommont;
import com.hdkj.eduservice.entity.EduArticle;
import com.hdkj.eduservice.entity.Uartcomm;
import com.hdkj.eduservice.service.ArtCommentService;
import com.hdkj.eduservice.service.ArticleService;
import com.hdkj.eduservice.service.UartcommService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/eduservice/uartcomm")
public class UartCommController {

    @Autowired
    private UartcommService uartcommService;

    @Autowired
    private ArtCommentService artCommentService;

    @Autowired
    private ArticleService articleService;


    @GetMapping("getuartclist")
    public R getuartclist(HttpServletRequest request) {
        String id = JwtUtils.getMemberIdByJwtToken(request);
        List conlist = new ArrayList();
        List nicklist = new ArrayList();
        List avarlist = new ArrayList();
        HashMap<String, Object> map = null;
        try {
            List<Uartcomm> uartcommList = uartcommService.list(new QueryWrapper<Uartcomm>().ne("userid", id));
            uartcommList.stream()
                    .forEach(o -> {
                        EduArtcommont artcommont = artCommentService.getOne(new QueryWrapper<EduArtcommont>().eq("id", o.getUserArtcommid()));
                        EduArticle article = articleService.getOne(new QueryWrapper<EduArticle>().eq("id", artcommont.getArticleId()));
                        if (article.getUserId().equals(id)) {
                            String content = artcommont.getContent();
                            String nickname = artcommont.getNickname();
                            String avartar = artcommont.getAvartar();
                            conlist.add(content);
                            nicklist.add(nickname);
                            avarlist.add(avartar);
                        }
                    });

            map = new HashMap<>();
            map.put("contentlist", conlist);
            map.put("nicklist", nicklist);
            map.put("avarlist", avarlist);
            map.put("items", uartcommList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().data("map", map);
    }


    @GetMapping("getarticleid/{index}")
    public R getarticleid(@PathVariable String index, HttpServletRequest request) {

        String id = JwtUtils.getMemberIdByJwtToken(request);
        List<Uartcomm> uartcommList = uartcommService.list(new QueryWrapper<Uartcomm>().ne("userid", id));
        Uartcomm uartcomm = uartcommList.get(Integer.parseInt(index));


        EduArtcommont artcommont = artCommentService.getOne(new QueryWrapper<EduArtcommont>().eq("id", uartcomm.getUserArtcommid()));

        String articleId = artcommont.getArticleId();


        return R.ok().data("aid", articleId);
    }


    @GetMapping("delUartcomm/{index}")
    public R deluartcomm(@PathVariable int index, HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        List<Uartcomm> uartcommList = uartcommService.list(new QueryWrapper<Uartcomm>().ne("userid", uid));
        Uartcomm uartcomm = uartcommList.get(index);
        uartcommService.removeById(uartcomm.getId());
        return R.ok();
    }


}
